package com.soft.servlet;

import com.soft.entity.UserInfo;
import com.soft.service.Domain;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserUpdateServlet",urlPatterns = "/UserUpdate")
public class UserUpdateServlet extends HttpServlet {

    private Domain domain;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        domain = new Domain();
        UserInfo ui = null;

        if (request.getSession().getAttribute("userAccount") != null){
            String account = request.getSession().getAttribute("userAccount").toString();
            ui = domain.getUserInfo(account);
            request.setAttribute("UserInfo", ui);
            request.getRequestDispatcher("/huiyuan.jsp").forward(request, response);
        } else {
            request.getSession().setAttribute("flag", 11);
            response.sendRedirect("denglu.jsp");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        String userId = request.getSession().getAttribute("userAccount").toString();
        String userName = request.getParameter("member.realname");
        String address = request.getParameter("member.city");
        String postcode = request.getParameter("member.postcode");
        String cardNum = request.getParameter("member.cardno");
        String IdType = request.getParameter("member.cardtype");
        String telephone = request.getParameter("member.tel");
        String email = request.getParameter("member.email");
        String password = request.getParameter("member.password");
        String newPassword = request.getParameter("newpassword");
        String renewPassword = request.getParameter("renewpassword");

        UserInfo ui = domain.getUserInfo(userId);
        ui.setName(userName);
        ui.setAddress(address);
        ui.setPostalCode(postcode);
        ui.setIdNumber(cardNum);
        ui.setIdType(IdType);
        ui.setTelephone(telephone);
        ui.setEmail(email);
        if (password != null && !password.equals("")) {
            if (!ui.getPassword().equals(password)) {
                request.getSession().setAttribute("flag", 9);
                doGet(request, response);
                response.sendRedirect("/huiyuan.jsp");
            }
            if (!newPassword.equals(renewPassword)) {
                request.getSession().setAttribute("flag", 10);
                doGet(request, response);
                response.sendRedirect("/huiyuan.jsp");
            }
            ui.setPassword(newPassword);
        }
        if (domain.updateUserInfo(ui)) {
            request.getSession().setAttribute("flag", 5);
        } else {
            request.getSession().setAttribute("flag", 6);
        }
        doGet(request, response);
    }
}
